﻿using System;
using System.Data;
using System.Linq;
using System.Text;

namespace DNA
{
    class tree_regress:tree
    {
        public int A = 1;
        public int G = 2;
        public int T = 3;
        public int C = 4;
        public int D = 5;
        public int I = 6;
        int say_row = 0;



        public void calc_me_a_correl()
        {
            tree our_tree = this.Owner as tree;
            preobrazvat();
            our_tree.calc_data.Rows.Clear();
            for (int elem1 = 0; elem1 < our_tree.data.Columns.Count; elem1++)
            {
                if (our_tree.data.Columns[elem1].HeaderText == "Диагноз") continue;
              
                for (int elem2 = 0; elem2 < our_tree.data.Columns.Count; elem2++)
                {
                    if (our_tree.data.Columns[elem2].HeaderText == "Диагноз") continue;
                    if (elem2 == elem1) continue;
                    corel_way(elem1, elem2);
                }
            }
                vernyt();
        }
        
        
        public void corel_way(int koren, int pretendent)
        {
            tree our_tree = this.Owner as tree;
            float summA = 0;
            float summB = 0;
            float sumA, sumB;
            float chislitel = 0;
            float dispersX = 0;
            float dispersY = 0;
            double znamenatel = 0;
            float korel = 0;
            bool happy = true;
            for (int i = 0; i < Convert.ToInt32(our_tree.eq_count.Value); i++)
            {
                try
                {
                    summA = summA + Convert.ToInt32(our_tree.data[koren, i].Value);
                    summB = summB + Convert.ToInt32(our_tree.data[pretendent, i].Value);
                }
                catch (System.Exception)
                {
                    happy = false;
                    break;
                }
            }
            if (happy == true)
            {
                sumA = summA / Convert.ToInt32(our_tree.eq_count.Value);
                sumB = summB / Convert.ToInt32(our_tree.eq_count.Value);
                for (int k = 0; k <Convert.ToInt32(our_tree.eq_count.Value); k++)
                {
                    chislitel = chislitel + (Convert.ToInt32(our_tree.data[koren, k].Value) - sumA) * (Convert.ToInt32(our_tree.data[pretendent, k].Value) - sumB);
                    dispersX = dispersX + (Convert.ToInt32(our_tree.data[koren, k].Value) - sumA) * (Convert.ToInt32(our_tree.data[koren, k].Value) - sumA);
                    dispersY = dispersY + (Convert.ToInt32(our_tree.data[pretendent, k].Value) - sumB) * (Convert.ToInt32(our_tree.data[pretendent, k].Value) - sumB);

                }
                znamenatel = Math.Sqrt(dispersX * dispersY);
                if (znamenatel != 0)
                {
                    korel = chislitel / (float)znamenatel;
                    our_tree.calc_data.Rows.Add(koren, pretendent);
                    our_tree.calc_data[2, say_row].Value = korel.ToString();
                    say_row++;
                }
            }
        }

        private void preobrazvat()
        {
            tree our_tree = this.Owner as tree;
            for (int i = 0; i < our_tree.data.Columns.Count; i++)
            {
                for (int j = 0; j < our_tree.data.Rows.Count; j++)
                {
                    try
                    {

                        if (our_tree.data[i, j].Value.ToString() == "М")
                        {
                            our_tree.data[i, j].Value = "3";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "Ж")
                        {
                            our_tree.data[i, j].Value = "4";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "AA")
                        {
                            our_tree.data[i, j].Value = A.ToString() + A.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "AG")
                        {
                            our_tree.data[i, j].Value = A.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "AC")
                        {
                            our_tree.data[i, j].Value = A.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GG")
                        {
                            our_tree.data[i, j].Value = G.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GA")
                        {
                            our_tree.data[i, j].Value = G.ToString() + A.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GT")
                        {
                            our_tree.data[i, j].Value = G.ToString() + T.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "GC")
                        {
                            our_tree.data[i, j].Value = G.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "TC")
                        {
                            our_tree.data[i, j].Value = T.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "TG")
                        {
                            our_tree.data[i, j].Value = T.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CA")
                        {
                            our_tree.data[i, j].Value = C.ToString() + A.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CG")
                        {
                            our_tree.data[i, j].Value = C.ToString() + G.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CT")
                        {
                            our_tree.data[i, j].Value = C.ToString() + T.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "TT")
                        {
                            our_tree.data[i, j].Value = T.ToString() + T.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "CC")
                        {
                            our_tree.data[i, j].Value = C.ToString() + C.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "DD")
                        {
                            our_tree.data[i, j].Value = D.ToString() + D.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "II")
                        {
                            our_tree.data[i, j].Value = I.ToString() + I.ToString();
                        }
                        if (our_tree.data[i, j].Value.ToString() == "ID")
                        {
                            our_tree.data[i, j].Value = I.ToString() + D.ToString();
                        }
                    }
                    catch
                    { continue; }
                }
            }
        }

        private void vernyt()
        {
            tree our_tree = this.Owner as tree;
            for (int i = 0; i < our_tree.data.Columns.Count; i++)
            {
                for (int j = 0; j < our_tree.data.Rows.Count; j++)
                {
                    try
                    {
                        if (our_tree.data.Columns[i].HeaderText == "ожирение" || our_tree.data.Columns[i].HeaderText == "курение" || our_tree.data.Columns[i].HeaderText == "алкоголь" || our_tree.data.Columns[i].HeaderText == "возраст") continue;
                        if (our_tree.data[i, j].Value.ToString() == "3")
                        {
                            our_tree.data[i, j].Value = "М";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "4")
                        {
                            our_tree.data[i, j].Value = "Ж";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "11")
                        {
                            our_tree.data[i, j].Value = "AA";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "12")
                        {
                            our_tree.data[i, j].Value = "AG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "14")
                        {
                            our_tree.data[i, j].Value = "AC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "22")
                        {
                            our_tree.data[i, j].Value = "GG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "21")
                        {
                            our_tree.data[i, j].Value = "GA";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "23")
                        {
                            our_tree.data[i, j].Value = "GT";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "24")
                        {
                            our_tree.data[i, j].Value = "GC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "34")
                        {
                            our_tree.data[i, j].Value = "TC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "32")
                        {
                            our_tree.data[i, j].Value = "TG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "41")
                        {
                            our_tree.data[i, j].Value = "CA";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "42")
                        {
                            our_tree.data[i, j].Value = "CG";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "43")
                        {
                            our_tree.data[i, j].Value = "CT";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "33")
                        {
                            our_tree.data[i, j].Value = "TT";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "44")
                        {
                            our_tree.data[i, j].Value = "CC";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "55")
                        {
                            our_tree.data[i, j].Value = "DD";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "66")
                        {
                            our_tree.data[i, j].Value = "II";
                        }
                        if (our_tree.data[i, j].Value.ToString() == "65")
                        {
                            our_tree.data[i, j].Value = "ID";
                        }
                    }
                    catch
                    { continue; }
                }
            }
        }

    }
}
